// var express = require('express');
import express from 'express';
import svgCaptcha from 'svg-captcha';

import { SYSTEM } from '../../config/Config';
import User from '../../entity/system/User';
import UserService from '../../service/system/UserService';
import BaseController from '../BaseController';

/**
 * 登陆接口
 */
export class RegisterController extends BaseController<null> {
    /**
     * 用户服务
     */
    useService: UserService;
    constructor() {
        super();
        this.useService = new UserService();
    }
    /**
     * 生成验证码
     */
    captcha(req: express.Request, res: express.Response, next: express.NextFunction) {
        var captcha = svgCaptcha.create();
        req.session.captcha = captcha.text;
        req.session.captchaData = new Date().getTime();
        res.type('svg');
        res.status(200).send(captcha.data);
    }
    /**
     * 用户注册功能
     */
    async register(req: express.Request, res: express.Response, next: express.NextFunction) {
        /**
         * 1.检测参数是否合法，账号、密码、验证码等不能为空
         * 2.校验验证码是否合法：验证码是否正确、验证码是否过期
         * 3.检测账号是否重复
         * 4.保存账号信息
         */
        try {
            //验证码校验
            let captcha = req.body.captcha;
            if (!captcha) {
                res.status(400).send({
                    code: 400,
                    msg: '验证码不能为空'
                })
                return;
            }
            //参数校验
            let user: User = new User();
            user.account = req.body.account;
            user.name = req.body.name;
            user.password = req.body.password;
            user.sex = req.body.sex;
            user.phone = req.body.phone;
            if (!user || !user.account || !user.name || !user.password || !user.phone) {
                res.status(400).send({
                    code: 400,
                    msg: '参数不合法'
                })
                return;
            }
            //验证码校验
            let systemCaptcha = req.session.captcha;
            let systemCaptchaDate = req.session.captchaData;
            if (!systemCaptcha || !systemCaptchaDate) {
                res.status(500).send({
                    code: 500,
                    msg: '验证码不合法'
                })
                return;
            }
            if (systemCaptcha !== captcha) {
                res.status(500).send({
                    code: 500,
                    msg: '验证码错误'
                })
                return;
            }
            if (new Date().getTime() - systemCaptchaDate > SYSTEM.captchaTimeOut) {
                res.status(500).send({
                    code: 500,
                    msg: '验证码已失效'
                })
                return;
            }
            //账号校验
            let systemUser = await this.useService.findByAccount(user.account);
            if (systemUser && systemUser.id) {
                res.status(500).send({
                    code: 500,
                    msg: '改账号已注册，无法重复注册'
                })
                return;
            }
            //电话号码检测
            systemUser = await this.useService.findByPhone(user.phone);
            if (systemUser && systemUser.id) {
                res.status(500).send({
                    code: 500,
                    msg: '改账号已注册，无法重复注册'
                })
                return;
            }
            this.useService.save(user);
            res.send({
                code: 200,
                msg: '注册成功'
            });
        } catch (error) {
            console.error(error);
            res.status(503).send({
                code: 503,
                msg: error
            })
            return;
        }
    }
}

var router = express.Router();
const registerController = new RegisterController();

//注册接口
router.post('/register', registerController.register.bind(registerController));

//获取验证码
router.get('/register/captcha', registerController.captcha.bind(registerController));
// module.exports = router;
export default router;
